/*

Copyright (C) 2010 Hans-Werner Hilse <hilse@web.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

*/

package de.hilses.droidreader;

import android.graphics.Matrix;
import android.graphics.Rect;

/**
 * Object that controls rendering parts of pages to a int[] pixmap buffer
 */
public class PdfView {
	protected static final long mInvertDisplayMask = 1;

	/**
	 * the pixmap we will render to
	 */
	public int[] mBuf;

	public final Rect mViewBox = new Rect();

	private int[] mRect = { 0, 0, 0, 0 };
	private float[] mMatrixSource = {
			0, 0, 0,
			0, 0, 0,
			0, 0, 0   };
	private float[] mMatrix = { 0, 0, 0, 0, 0, 0 };

	protected boolean mDisplayInvert = false;

	/**
	 * Call native code to render part of a page to a buffer
	 * @param dochandle the handle of the document for which we render
	 * @param pagehandle the handle of the page for which we render
	 * @param viewbox the excerpt that we should render, given as int[4] rectangle
	 * @param matrix the transformation matrix used for rendering, MuPDF format (float[6])
	 * @param buffer the int[] buffer we render to
	 */
	private native void nativeCreateView(
			long dochandle, long pagehandle,
			int[] viewbox, float[] matrix, int[] buffer, long flags)
		throws PageRenderException;

	/**
	 * Render part of the page to a int[] buffer
	 * @param page the PdfPage we render for
	 * @param viewbox the excerpt Rect that we should render (coordinates after applying the matrix)
	 * @param matrix the Matrix used for rendering
	 */
	public void render(PdfDocument doc, PdfPage page, Rect viewbox, Matrix matrix)
			throws PageRenderException
	{
		int size = viewbox.width() * viewbox.height()
				* ((PdfRender.bytesPerPixel * 8) / 32);

		long tempflags;

		if((mBuf == null) || (mBuf.length != size))
			mBuf = new int[size];

		mRect[0] = viewbox.left;
		mRect[1] = viewbox.top;
		mRect[2] = viewbox.right;
		mRect[3] = viewbox.bottom;

		matrix.getValues(mMatrixSource);
		mMatrix[0] = mMatrixSource[0];
		mMatrix[1] = mMatrixSource[3];
		mMatrix[2] = mMatrixSource[1];
		mMatrix[3] = mMatrixSource[4];
		mMatrix[4] = mMatrixSource[2];
		mMatrix[5] = mMatrixSource[5];

		tempflags = 0;
		if (mDisplayInvert)
			tempflags |= mInvertDisplayMask;

		this.nativeCreateView(
				doc.mHandle, page.mHandle,
				mRect, mMatrix, mBuf, tempflags);
		mViewBox.set(viewbox);
	}

	public void setDisplayInvert(boolean invert) {
		mDisplayInvert = invert;
	}
}
